﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Config_App
{
    /// <summary>
    /// Benutzersteuerelement für die Approximation
    /// </summary>
    public partial class _uc_APPROX : UserControl
    {
        private delegate void _del_SET_TXT(string ITERA, string ERR);
        private delegate void _del_EnableControls(bool enable);

        public double[,] data = null;
        
        public float[][] input;
        public float[][] output;

        public _uc_APPROX()
        {
            InitializeComponent();

            // init chart control
            this._chart.AddDataSeries("data", Color.Red, AForge.Controls.Chart.SeriesType.Dots, 5);
            this._chart.AddDataSeries("solution", Color.Blue, AForge.Controls.Chart.SeriesType.ConnectedDots, 5);
        }

        /// <summary>
        /// Lädt eine Datei in data und zeigt alle dialoge an
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _b_load_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Datendateien|*.csv";
            ofd.InitialDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(_uc_APPROX)).Location) + "\\BEISPIELE\\APPROX\\";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamReader reader = null;
                // lese maximal 50 punkte
                double[,] tempData = new double[50, 2];
                
                try
                {
                    reader = System.IO.File.OpenText(ofd.FileName);
                    string str = null;
                    int i = 0;

                    // lese daten
                    while ((i < 50) && ((str = reader.ReadLine()) != null))
                    {
                        string[] strs = str.Split(';');
                        if (strs.Length == 1)
                            strs = str.Split(',');
                        // parse X
                        tempData[i, 0] = double.Parse(strs[0]);
                        tempData[i, 1] = double.Parse(strs[1]);

                        i++;
                    }

                    data = new double[i, 2];
                    Array.Copy(tempData, 0, data, 0, i * 2);
                }
                catch (Exception)
                {
                    MessageBox.Show("Fehler beim parsen der Datei!", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                }

                UpdateDataListView();
            }
        }

        /// <summary>
        /// Aktualisert die Daten in der LV
        /// </summary>
        public void UpdateDataListView()
        {
            this._lv_data.Items.Clear();
         
            for (int i = 0, n = data.GetLength(0); i < n; i++)
            {
                this._lv_data.Items.Add(data[i, 0].ToString());
                this._lv_data.Items[i].SubItems.Add(data[i, 1].ToString());
            }

            double minX = double.MaxValue;
            double maxX = double.MinValue;

            for (int i = 0; i < data.GetLength(0); i++)
            {
                // suche min wert
                if (data[i, 0] < minX)
                    minX = data[i, 0];
                // suche max wert
                if (data[i, 0] > maxX)
                    maxX = data[i, 0];
            }

            this._chart.RangeX = new AForge.Range((float)minX, (float)maxX);
            this._chart.UpdateDataSeries("data", data);
            this._chart.UpdateDataSeries("solution", null);
        }

        /// <summary>
        /// Erzeugt und setzt die Konfiguration zur abholung
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _cmd_ok_Click(object sender, EventArgs e)
        {
            int samples = data.GetLength(0);
            // daten transformationsfaktor
            double yFactor = 1.7 / this._chart.RangeY.Length;
            double yMin = this._chart.RangeY.Min;
            double xFactor = 2.0 / this._chart.RangeX.Length;
            double xMin = this._chart.RangeX.Min;

            // erzeuge arrays
            input = new float[samples][];
            output = new float[samples][];
            
            for (int i = 0; i < samples; i++)
            {
                input[i] = new float[1];
                output[i] = new float[1];

                input[i][0] = (float)((data[i, 0] - xMin) * xFactor - 1.0);
                output[i][0] = (float)((data[i, 1] - yMin) * yFactor - 0.85);
            }
        }
    }
}
